package com.mapapp.flutter_mapapp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.mapapp.flutter_mapapp.MainActivity;
import com.mapapp.flutter_mapapp.R;
import com.mapapp.flutter_mapapp.persenter.FindPasswordPersenter;
import com.mapapp.flutter_mapapp.util.MyActivityManager;
import com.mapapp.flutter_mapapp.util.ToastUtils;
import com.mapapp.flutter_mapapp.view.FindPasswordViewInterfece;
import com.mapapp.flutter_mapapp.widget.MyCountDownTimer;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FindPasswordActivity extends ImmersiveBaseActivity implements View.OnClickListener , FindPasswordViewInterfece{
    private static final String TAG = "FindPasswordActivity";

    @BindView(R.id.iv_find_password_back)
    ImageView ivFindPasswordBack;
    @BindView(R.id.et_find_password_phone)
    EditText etFindPasswordPhone;
    @BindView(R.id.et_find_password_code)
    EditText etFindPasswordCode;
    @BindView(R.id.btn_find_password_send_code)
    Button btnFindPasswordSendCode;
    @BindView(R.id.btn_find_password_reset)
    Button btnFindPasswordReset;
    @BindView(R.id.btn_find_password_cancel)
    Button btnFindPasswordCancel;

    private MyCountDownTimer timer;
    private FindPasswordPersenter mPersenter;
    private String mPhone;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_password);
        ButterKnife.bind(this);

        mContext = this;
        init();
        mPersenter = new FindPasswordPersenter(this,this,lifecycleSubject);
    }

    private void init(){
        ivFindPasswordBack.setOnClickListener(this);
        btnFindPasswordSendCode.setOnClickListener(this);
        btnFindPasswordReset.setOnClickListener(this);
        btnFindPasswordCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_find_password_back:
                MyActivityManager.getActivityManager().pop(this);
                break;
            case R.id.btn_find_password_send_code:
                String phone = etFindPasswordPhone.getText().toString().trim();
                if(!"".equals(phone) && phone.length() == 11){
                    mPhone = phone;
                    mPersenter.getCode(phone);
                }else{
                    ToastUtils.showMessage(mContext, "请输入正确的手机号");
                }
                break;
            case R.id.btn_find_password_reset:
                String code = etFindPasswordCode.getText().toString().trim();
                mPersenter.checkCode(mPhone,code);
                break;
            case R.id.btn_find_password_cancel:
                MyActivityManager.getActivityManager().pop(this);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(timer!=null){
            timer.cancel();
            timer.onFinish();
        }
    }

    @Override
    public void getCodeSuccess() {
        timer = new MyCountDownTimer(mContext, 60000, 1000,
                btnFindPasswordSendCode, mContext.getString(R.string.send_verification_code));
        timer.start();
        hintKbTwo();
    }

    @Override
    public void checkCodeSuccess(String tempToken) {
        Intent intent = new Intent(mContext, ResetPasswordActivity.class);
        intent.putExtra("phone",mPhone);
        intent.putExtra("tempToken",tempToken);
        startActivityForResult(intent,0);
        if(timer!=null){
            timer.cancel();
            timer.onFinish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0 && resultCode == 111){
            setResult(222);
            MyActivityManager.getActivityManager().pop(this);
        }
        if(requestCode == 0 && resultCode == 112){
            etFindPasswordPhone.setText("");
            etFindPasswordCode.setText("");
            btnFindPasswordSendCode.setText("发送验证码");
        }
    }

    protected void hintKbTwo() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive() && getCurrentFocus() != null) {
            if (getCurrentFocus().getWindowToken() != null) {
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }
}
